package com.moyq5.mvc.maker.test.bean.entity;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;

import com.moyq5.mvc.maker.test.bean.embed.Owner;
import com.moyq5.mvc.maker.test.bean.enums.VirtualTradeBiz;
import com.moyq5.mvc.maker.test.bean.enums.VirtualTradeRefType;
import com.moyq5.mvc.maker.test.bean.enums.VirtualTradeStatus;
import com.moyq5.mvc.maker.test.bean.enums.VirtualTradeType;

/**
 * 虚拟账户交易记录（充值\提现）
 * 
 * @author Moyq5
 * @date 2017年11月24日
 */
@Entity
@Table(name = "uc_virtual_trade",indexes={@Index(columnList="add_time")})
@DynamicUpdate
public class VirtualTrade implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	/**
	 * 交易ID（主键）
	 */
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")@GenericGenerator(name = "native", strategy = "native")
    @Column(name = "trade_id")
	private Long tradeId;
	/**
	 * 流水号
	 */
	@Column(name = "trade_no", length = 100, nullable = false, unique = true)
	private String tradeNo;
	/**
	 * 归属信息
	 */
	@Embedded
	private Owner owner;
	/**
	 * 交易账户
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "account_id", nullable = false)
	private VirtualAccount account;
	/**
	 * 交易金额（元）
	 */
    @Column(name = "amount", nullable = false, precision = 10, scale = 2)	
	private BigDecimal amount;
	/**
	 * 交易前余额（元）
	 */
    @Column(name = "amount_before", nullable = false, precision = 10, scale = 2)	
	private BigDecimal amountBefore;
	/**
	 * 交易后余额（元）
	 */
    @Column(name = "amount_after", nullable = false, precision = 10, scale = 2)	
	private BigDecimal amountAfter;
	/**
	 * 交易状态
	 */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "status", precision = 2, nullable = false, columnDefinition="int(1) default '0'")
	private VirtualTradeStatus status;
    /**
	 * 业务类型
	 */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "biz", precision = 2, nullable = false, columnDefinition="int(1) default '0'")
	private VirtualTradeBiz biz;
    /**
     * 业务单号
     */
    @Column(name = "biz_no", length = 100)
    private String bizNo;
    /**
     * 业务名称
     */
    @Column(name = "biz_name", length = 100)
    private String bizName;
    /**
	 * 收支类型
	 */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "type", precision = 2, nullable = false, columnDefinition="int(1)")
	private VirtualTradeType type;
    /**
     * 源数据类型
     */
    @Column(name = "ref_type", precision = 2, nullable = false, columnDefinition="int(1)")
    @Enumerated(EnumType.ORDINAL)
    private VirtualTradeRefType refType;
    /**
	 * 退款信息
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "refund_id")
	private TraRefund refund;
	/**
	 * 出款信息
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "remit_id")
	private TraRemit remit;
	/**
	 * 交易信息（消费者订单信息）
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "order_id")
	private UserOrder userOrder;
	/**
	 * 分润信息
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "profit_id")
	private TraProfit profit;
	/**
	 * 资金变动说明（备注信息）
	 */
	@Column(name = "remark", length = 300, nullable = true)
	private String remark;
	/**
	 * 添加时间（交易时间）
	 */
	@Column(name = "add_time", nullable = false, columnDefinition="datetime")
	private Date addTime;
	
	
	@PrePersist
	public void prePersist() {
		tradeNo = bizNo + 
				"-" + biz.ordinal() + 
				"-" + amount.multiply(new BigDecimal("100")).setScale(0, RoundingMode.DOWN) + 
				"-" + account.getAccountId() + 
				"-" + type.ordinal(); 
		if (null == addTime) {
			addTime = new Date();
		}
	}
	public Long getTradeId() {
		return tradeId;
	}
	public void setTradeId(Long tradeId) {
		this.tradeId = tradeId;
	}
	public VirtualAccount getAccount() {
		return account;
	}
	public void setAccount(VirtualAccount account) {
		this.account = account;
	}
	public BigDecimal getAmount() {
		return amount;
	}
	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}
	public BigDecimal getAmountBefore() {
		return amountBefore;
	}
	public void setAmountBefore(BigDecimal amountBefore) {
		this.amountBefore = amountBefore;
	}
	public BigDecimal getAmountAfter() {
		return amountAfter;
	}
	public void setAmountAfter(BigDecimal amountAfter) {
		this.amountAfter = amountAfter;
	}
	public VirtualTradeStatus getStatus() {
		return status;
	}
	public void setStatus(VirtualTradeStatus status) {
		this.status = status;
	}
	public VirtualTradeType getType() {
		return type;
	}
	public void setType(VirtualTradeType type) {
		this.type = type;
	}
	public TraRemit getRemit() {
		return remit;
	}
	public void setRemit(TraRemit remit) {
		this.remit = remit;
	}
	public UserOrder getUserOrder() {
		return userOrder;
	}
	public void setUserOrder(UserOrder userOrder) {
		this.userOrder = userOrder;
	}
	public TraProfit getProfit() {
		return profit;
	}
	public void setProfit(TraProfit profit) {
		this.profit = profit;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public Date getAddTime() {
		return addTime;
	}
	public void setAddTime(Date addTime) {
		this.addTime = addTime;
	}
	public VirtualTradeRefType getRefType() {
		return refType;
	}
	public void setRefType(VirtualTradeRefType refType) {
		this.refType = refType;
	}
	public VirtualTradeBiz getBiz() {
		return biz;
	}
	public void setBiz(VirtualTradeBiz biz) {
		this.biz = biz;
	}
	public String getBizName() {
		return bizName;
	}
	public void setBizName(String bizName) {
		this.bizName = bizName;
	}
	public String getBizNo() {
		return bizNo;
	}
	public void setBizNo(String bizNo) {
		this.bizNo = bizNo;
	}
	public TraRefund getRefund() {
		return refund;
	}
	public void setRefund(TraRefund refund) {
		this.refund = refund;
	}
	public Owner getOwner() {
		return owner;
	}
	public void setOwner(Owner owner) {
		this.owner = owner;
	}
	public String getTradeNo() {
		return tradeNo;
	}
	public void setTradeNo(String tradeNo) {
		this.tradeNo = tradeNo;
	}

}
